package cc.dullsky.music;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import cc.dullsky.common.Constant;
import cc.dullsky.common.model.Music;
import cc.dullsky.common.model.Musiccat;
import cc.dullsky.common.model.User;
import cc.dullsky.interceptor.AuthInterceptor;
import cc.dullsky.util.NetEaseUtil;
import cc.dullsky.util.RegexpUtil;

@Before(AuthInterceptor.class)
public class MusicController extends Controller {
	@Clear
	public void getMusic() {
		List<Map<String, Object>> list = new ArrayList<>();
		List<Music> musics = Music.dao.findAll();
		for (Music music : musics) {
			Map<String, Object> map = new HashMap<>();
			map.put("title", music.getSong());
			map.put("author", music.getSinger());
			map.put("url", music.getMp3url());
			map.put("pic", music.getPicurl());
			map.put("lrc", music.getLrc());
			list.add(map);
		}
		renderJson(list);
	}

	public void index() {
		User user = getSessionAttr("user");
		List<Musiccat> musiccats = Musiccat.dao.findAll(user.getId());
		setAttr("list", musiccats);
	}

	public void search() {
		String song = getPara("song");
		int catId = getParaToInt("catId");
		int pageNumber = getParaToInt("pageNumber");
		int pageSize = getParaToInt("pageSize");
		User user = getSessionAttr("user");
		Page<Music> page = Music.dao.search(pageNumber, pageSize, user.getId(), song, catId);
		List<Music> musics = page.getList();
		List<Map<String, Object>> list = new ArrayList<>();
		for (Music music : musics) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", music.getId());
			map.put("song", music.getSong());
			map.put("singer", music.getSinger());
			if (music.getMusiccatId() == 0) {
				map.put("category", "未分类");
			} else {
				map.put("category", Musiccat.dao.findById(music.getMusiccatId()).getName());
			}
			map.put("neteaseid", music.getNeteaseid());
			list.add(map);
		}
		setAttr("rows", list);
		setAttr("page", page.getTotalPage());
		setAttr("total", page.getTotalRow());
		renderJson();
	}

	public void add() {
		User user = getSessionAttr("user");
		int catId = getParaToInt("catId");
		String link = getPara("link");
		int type;// 1song,2album,3playlist
		if (link.indexOf("song") > -1) {
			type = 1;
		} else if (link.indexOf("album") > -1) {
			type = 2;
		} else if (link.indexOf("playlist") > -1) {
			type = 3;
		} else {
			renderJson(2);
			return;
		}
		String netEaseId = RegexpUtil.getIdFromLink(link);
		if (netEaseId == null) {
			renderJson(2);
			return;
		}
		JSONArray songs = null;
		JSONObject jsonObject = null;
		if (type == 1) {
			jsonObject = NetEaseUtil.doGetStr(Constant.NETEASE_SONG.replace("ID", netEaseId));
			songs = jsonObject.getJSONArray("songs");
		} else if (type == 2) {
			jsonObject = NetEaseUtil.doGetStr(Constant.NETEASE_ALBUM.replace("ID", netEaseId));
			songs = jsonObject.getJSONObject("album").getJSONArray("songs");
		} else if (type == 3) {
			jsonObject = NetEaseUtil.doGetStr(Constant.NETEASE_PLAYLIST.replace("ID", netEaseId));
			songs = jsonObject.getJSONObject("result").getJSONArray("tracks");
		}
		JSONObject songi = null;
		int flag = 0;
		for (int i = 0; i < songs.size(); i++) {
			songi = songs.getJSONObject(i);
			JSONObject lrcJsonObject = NetEaseUtil.doGetStr(Constant.NETEASE_LYRIC.replace("ID", songi.getString("id")));
			String lrc = "[00:00.00]暂无歌词";
			if (lrcJsonObject.getJSONObject("lrc") != null) {
				lrc = lrcJsonObject.getJSONObject("lrc").getString("lyric");
			}
			boolean boo = new Music().set("neteaseid", songi.getString("id")).set("mp3url", songi.getString("mp3Url"))
					.set("song", songi.getString("name")).set("singer", songi.getJSONArray("artists").getJSONObject(0).getString("name"))
					.set("picurl", songi.getJSONObject("album").getString("picUrl")).set("musiccat_id", catId).set("user_id", user.getId())
					.set("lrc", lrc).save();
			if (boo) {
				flag++;
			}
		}
		if (flag == 0) {
			renderJson(0);
		} else {
			renderJson(1);
		}
	}

	public void deleteByIds() {
		String ids = getPara("ids");
		String[] idArray = ids.split(",");
		for (String id : idArray) {
			Music.dao.deleteById(id);
		}
		renderJson();
	}

	public void deleteById() {
		Music.dao.deleteById(getParaToInt("id"));
		renderJson();
	}

}
